﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mdb2RIA
{
    public static class Utilities
    {
        public static void AppendMultipleLines(StringBuilder stringBuilder, params string[] lines)
        {
            foreach (string line in lines)
            {
                stringBuilder.AppendLine(line);
            }
        }

        private static string GenerateIndentation(int openBracketCount)
        {
            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < openBracketCount; i++)
            {
                builder.Append("    ");
            }

            return builder.ToString();
        }

        public static string FormatDocument(string document)
        {
            int openBracketCount = 0;

            StringBuilder formattedDocument = new StringBuilder();

            string[] documentLines = document.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            foreach (string line in documentLines)
            {
                bool dontTouchIdent = false;

                if ( (line.Contains('}')) && (line.Contains('{')) )
                {
                    dontTouchIdent = true;
                }

                if (!dontTouchIdent)
                {
                    if (line.Contains('}'))
                    {
                        openBracketCount--;
                    }
                }

                formattedDocument.AppendLine(GenerateIndentation(openBracketCount) + line);

                if (!dontTouchIdent)
                {
                    if (line.Contains('{'))
                    {
                        openBracketCount++;
                    }
                }
            }

            return formattedDocument.ToString();
        }
    }
}
